package com.snow.useviewdemo.function2.radius;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import static org.xclcharts.common.DensityUtil.dip2px;

/**
 * Created by zhaoyt on 12/13/22.
 * Describe:
 */
public class DrawLineView extends View {
    /**
     * 上下文
     */
    private Context mContext = null;
    /**
     * 画图区域的宽高
     */
    private float width, height;
    /**
     * 纵坐标平均每一行的高度
     */
    private float fRowHeight;
    /**
     * 横坐标平均每一列的宽带
     */
    private float fColWidth = 0;
    /**纵向显示个数*/
//    private static final int VERTICALCOUNT = 5;
    /**横向显示个数*/
//    private static final int HORIZONTALCOUNT = 7;
    /**
     * 固定要显示的利率
     */
//    private static final int CONSTANT_DISPLAY_RATE = 8;
    /**
     * 横纵向字体大小单位dp
     */
    private static final int TEXTSIZE = 12;
    /**
     * 纵坐标、横坐标的字体画笔
     */
    private Paint textPaint = null;
    /**
     * 横坐标画笔
     */
    private Paint horizontalPaint = null;
    /**
     * 固定的虚线画笔
     */
    private Paint dottedLinePaint = null;
    /**
     * 画Y轴的刻度线
     */
    private Paint yLinePaint = null;
    /**
     * 画空心圆
     */
    private Paint circlePaint = null;
    /**
     * 圆的半径 单位dp
     */
    private int circleRadius = 3;
    /**
     * 折线画笔
     */
    private Paint linePaint = null;
    private Paint innerPaint = null;
    /**
     * 阴影画笔
     */
    private Paint shadowPaint = null;
    /**
     * 阴影走过的路径
     */
    private Path shadowPath = null;

    private List<Integer> yList = new ArrayList<Integer>();
    private List<String> xList = new ArrayList<String>();
    /**
     * Y轴最大值
     */
    private float maxY = 0;
    /**
     * 纵向设置的Y每一段的值
     */
    private int verticalAnnualValueY = 0;
    /**
     * 横行字体宽带
     */
    private float horizontalTextWidth = 0;

    /**
     * 横向起始位置
     */
    private float xStart = 0;
    private float[] xPoint;
    private float[] yPoint;

    public DrawLineView(Context context) {
        this(context, null);
    }

    public DrawLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initWillUsed(context);
    }

    /**
     * 初始化将使用到的参数
     */
    private void initWillUsed(Context context) {
        mContext = context;

        textPaint = new Paint();
        textPaint.setColor(Color.parseColor("#B9C7DD"));
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(dip2px(mContext, TEXTSIZE));

        horizontalPaint = new Paint();
        horizontalPaint.setColor(Color.parseColor("#B9C7DD"));
        horizontalPaint.setAntiAlias(true);
        horizontalPaint.setStrokeWidth(1);

        yLinePaint = new Paint();
        yLinePaint.setColor(Color.parseColor("#B9C7DD"));
        yLinePaint.setAntiAlias(true);

        dottedLinePaint = new Paint();
        dottedLinePaint.setStyle(Paint.Style.STROKE);
        dottedLinePaint.setColor(Color.parseColor("#C5D1FF"));
        dottedLinePaint.setAntiAlias(true);

        circlePaint = new Paint();
        circlePaint.setColor(Color.parseColor("#6789FD"));
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setAntiAlias(true);
        circlePaint.setStrokeWidth(1);

        linePaint = new Paint();
        linePaint.setColor(Color.parseColor("#6789FD"));
        linePaint.setStrokeWidth(2);
        linePaint.setAntiAlias(true);

        innerPaint = new Paint();
        innerPaint.setColor(Color.WHITE);
        innerPaint.setStyle(Paint.Style.FILL);
        innerPaint.setAntiAlias(true);


    }

    /**
     * 设置将要绘制的数据
     */
    public void setData(List<String> xValuesList, List<Integer> yValuesList, float currMaxY) {
        this.xList = xValuesList;
        this.yList = yValuesList;
        xPoint = new float[xList.size()];
        yPoint = new float[xList.size()];

        if (currMaxY / yList.size() == currMaxY % yList.size()) {
            verticalAnnualValueY = (int) currMaxY / yList.size();
        } else {
            verticalAnnualValueY = (int) currMaxY / yList.size() + 1;
        }
        maxY = yList.size() * verticalAnnualValueY;
        setWillNotDraw(false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        width = getWidth();
        height = getHeight();
        fRowHeight = (float) height / (yList.size() + 1);
        xStart = 0;
        fColWidth = 0;
        shadowPaint = new Paint();
        shadowPaint.setAntiAlias(true);
        shadowPath = new Path();
        drawLine(canvas);
    }

    /**
     * 画折线 和 阴影
     */
    private void drawLine(Canvas canvas) {
        int maxHeightPoint = 0;//记录最高的点
        float maxHeight = 0;
        for (int i = 0; i < xList.size(); i++) {
            xPoint[i] = xStart + i * fColWidth;

            if (maxHeight < yList.get(i)) {
                maxHeight = yList.get(i);
                maxHeightPoint = i;
            }
            yPoint[i] = (maxY - yList.get(i)) / maxY * yList.size() * fRowHeight + dip2px(mContext, TEXTSIZE);

            if (i > 0) {
                canvas.drawLine(xPoint[i - 1], yPoint[i - 1], xPoint[i], yPoint[i], linePaint);
                if (i == 1) {
                    shadowPath.moveTo(xPoint[i - 1], yPoint[i - 1]);
                }
                shadowPath.lineTo(xPoint[i], yPoint[i]);
            }
        }
        shadowPath.lineTo(xPoint[xList.size() - 1], yList.size() * fRowHeight);
        shadowPath.lineTo(xPoint[0], yList.size() * fRowHeight);
        shadowPath.close();
        Shader lShader = new LinearGradient(0, yPoint[maxHeightPoint], 0, yList.size() * fRowHeight, Color.parseColor("#BFC5D1FF"), Color.parseColor("#BFFDFDFF"), Shader.TileMode.REPEAT);
        shadowPaint.setShader(lShader);
        canvas.drawPath(shadowPath, shadowPaint);
    }
}
